package com.xuelang.pipeline.artifact.infrastructure.resource.repository;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xuelang.pipeline.artifact.client.common.constant.Constant;
import com.xuelang.pipeline.artifact.client.common.result.PageRecord;
import com.xuelang.pipeline.artifact.client.resource.dto.ArtifactNamespaceQuery;
import com.xuelang.pipeline.artifact.domain.resource.ArtifactNamespace;
import com.xuelang.pipeline.artifact.domain.resource.repository.ArtifactNamespaceRepository;
import com.xuelang.pipeline.artifact.infrastructure.resource.converter.ArtifactNamespaceToArtifactNamespaceDOConverter;
import com.xuelang.pipeline.artifact.infrastructure.resource.dataobject.ArtifactNamespaceDO;
import com.xuelang.pipeline.artifact.infrastructure.resource.mapper.ArtifactNamespaceMapper;
import dm.jdbc.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 镜像仓库命名空间仓库实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Slf4j
@Repository
public class ArtifactNamespaceRepositoryImpl implements ArtifactNamespaceRepository {

    @Autowired
    ArtifactNamespaceMapper artifactNamespaceMapper;

    @Override
    public PageRecord<ArtifactNamespace> pageList(long currentPage, long pageSize, ArtifactNamespaceQuery artifactNamespaceQuery) {
        currentPage = currentPage < 1 ? 1 : currentPage;
        pageSize = pageSize < 1 ? 10 : pageSize;
        Page<ArtifactNamespace> page = new Page<>(currentPage, pageSize);
        MPJLambdaWrapper<ArtifactNamespaceDO> wrapper = new MPJLambdaWrapper<ArtifactNamespaceDO>();
        wrapper.selectAll(ArtifactNamespaceDO.class)
                .eq(ArtifactNamespaceDO::getArtifactUuid, StrUtil.isNotBlank(artifactNamespaceQuery.getArtifactUuid()) ? artifactNamespaceQuery.getArtifactUuid() : Constant.DEFAULT_DB_NOT_EXISTS_DATA);
        wrapper.and(StrUtil.isNotBlank(artifactNamespaceQuery.getCondition()), wp -> wp
                .like(ArtifactNamespaceDO::getName, artifactNamespaceQuery.getCondition()).or()
                .like(ArtifactNamespaceDO::getAccessLevel, artifactNamespaceQuery.getCondition()).or()
                .like(ArtifactNamespaceDO::getUuid, artifactNamespaceQuery.getCondition()))
                .apply(StrUtil.isNotBlank(artifactNamespaceQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", artifactNamespaceQuery.getStartDate())
                .apply(StrUtil.isNotBlank(artifactNamespaceQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", artifactNamespaceQuery.getEndDate())
                .orderByDesc(ArtifactNamespaceDO::getCreateTime);

        artifactNamespaceMapper.selectJoinPage(page, ArtifactNamespace.class, wrapper);
        return new PageRecord<>(
                page.getCurrent(),
                page.getSize(),
                page.getTotal(),
                page.getPages(),
                page.getRecords());
    }

    @Override
    public List<ArtifactNamespace> list(ArtifactNamespaceQuery artifactNamespaceQuery) {
        MPJLambdaWrapper<ArtifactNamespaceDO> wrapper = new MPJLambdaWrapper<ArtifactNamespaceDO>();
        wrapper.selectAll(ArtifactNamespaceDO.class)
                .eq(ArtifactNamespaceDO::getArtifactUuid, StrUtil.isNotBlank(artifactNamespaceQuery.getArtifactUuid()) ? artifactNamespaceQuery.getArtifactUuid() : Constant.DEFAULT_DB_NOT_EXISTS_DATA);
        wrapper.and(StrUtil.isNotBlank(artifactNamespaceQuery.getCondition()), wp -> wp
                        .like(ArtifactNamespaceDO::getName, artifactNamespaceQuery.getCondition()).or()
                        .like(ArtifactNamespaceDO::getAccessLevel, artifactNamespaceQuery.getCondition()).or()
                        .like(ArtifactNamespaceDO::getUuid, artifactNamespaceQuery.getCondition()))
                .apply(StrUtil.isNotBlank(artifactNamespaceQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')>={0}", artifactNamespaceQuery.getStartDate())
                .apply(StrUtil.isNotBlank(artifactNamespaceQuery.getStartDate()), "DATE_FORMAT(t.create_time,'%Y-%m-%d')<={0}", artifactNamespaceQuery.getEndDate())
                .orderByDesc(ArtifactNamespaceDO::getCreateTime);

        return artifactNamespaceMapper.selectJoinList(ArtifactNamespace.class, wrapper);
    }

    @Override
    public Boolean save(ArtifactNamespace artifactNamespace) {
        return artifactNamespaceMapper.insert(ArtifactNamespaceToArtifactNamespaceDOConverter.INSTANCE.artifactNamespaceToArtifactNamespaceDO(artifactNamespace)) == 1;
    }

    @Override
    public Boolean update(ArtifactNamespace artifactNamespace) {
        return artifactNamespaceMapper.update(
                ArtifactNamespaceToArtifactNamespaceDOConverter.INSTANCE.artifactNamespaceToArtifactNamespaceDO(artifactNamespace),
                Wrappers.lambdaQuery(ArtifactNamespaceDO.class)
                        .eq(ArtifactNamespaceDO::getUuid, artifactNamespace.getUuid())) == 1;
    }

    @Override
    public Boolean delete(String uuid) {
        return artifactNamespaceMapper.delete(Wrappers.lambdaQuery(ArtifactNamespaceDO.class)
                .eq(ArtifactNamespaceDO::getUuid, uuid)) == 1;
    }

    @Override
    public ArtifactNamespace query(String uuid) {
        ArtifactNamespaceDO artifactNamespaceDO = artifactNamespaceMapper.selectOne(Wrappers.lambdaQuery(ArtifactNamespaceDO.class).eq(ArtifactNamespaceDO::getUuid, uuid));
        return ArtifactNamespaceToArtifactNamespaceDOConverter.INSTANCE.artifactNamespaceDOToArtifactNamespace(artifactNamespaceDO);
    }

    @Override
    public Boolean existsName(String artifactUuid, String uuid, String name) {
        List<ArtifactNamespaceDO> artifactNamespaceDOS = artifactNamespaceMapper.selectList(Wrappers.lambdaQuery(ArtifactNamespaceDO.class)
                .eq(ArtifactNamespaceDO::getArtifactUuid, artifactUuid)
                .eq(ArtifactNamespaceDO::getName, name));
        if (ObjectUtil.isEmpty(artifactNamespaceDOS)) {
            return false;
        }
        return artifactNamespaceDOS.stream().anyMatch(artifactNamespaceDO -> !StringUtil.equals(uuid, artifactNamespaceDO.getUuid()));
    }
}
